﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


//FALTA LA PARTE DE IMPACTAR EN LA BASE DE DATOS.


namespace GrouponDesktop.ArmarCupon
{
    public partial class ArmarCupon : Form
    {

        int idProv;
        public ArmarCupon(int id)
        {
            this.idProv = id;
            InitializeComponent();
            BDConnection.FillCheckedList("select ciudad from CodingSpree.TL_CIUDADES", clb_zona);
            tb_fVenc.Format = DateTimePickerFormat.Custom;
            tb_fVenc.CustomFormat = "yyyy-MM-dd";
            tb_fVencC.Format = DateTimePickerFormat.Custom;
            tb_fVencC.CustomFormat = "yyyy-MM-dd";

        }

        private void b_armar_Click(object sender, EventArgs e)
        {

            if (!string.IsNullOrEmpty(tb_desc.Text))
            {
                if (!string.IsNullOrEmpty(tb_fVenc.Text))
                {
                    if (!string.IsNullOrEmpty(tb_fVencC.Text))
                    {
                        if (nu_cDisp.Value != 0)
                        {
                            if ((nu_cMax.Value != 0) && ( nu_cDisp.Value > nu_cMax.Value))
                            {
                                if (nu_pFict.Value != 0) 
                                {
                                    if ((nu_pReal.Value != 0) && (nu_pReal.Value <= nu_pFict.Value))
                                    {
                                        if (clb_zona.CheckedItems.Count != 0)
                                        {
                                            int verFec1 = BDConnection.QueryAndExecuteScalar("EXEC CodingSpree.verFecha2 '" + tb_fVenc.Text + "', '" + variablesGlobales.Configuracion.fechaHoy + "'");
                                            int verFec2 = BDConnection.QueryAndExecuteScalar("EXEC CodingSpree.verFecha2 '" + tb_fVencC.Text + "', '" + variablesGlobales.Configuracion.fechaHoy + "'");

                                            if (verFec1 == 1 && verFec2 == 1)
                                            {
                                                string command = "EXEC CodingSpree.insertCupon ";
                                                command = command + nu_pReal.Value + ", ";
                                                command = command + nu_pFict.Value + ", ";
                                                command = command + "NULL, '";
                                                command = command + tb_fVenc.Text + "', '";
                                                command = command + tb_fVencC.Text + "', ";
                                                command = command + nu_cDisp.Value + ", '";
                                                command = command + tb_desc.Text + "', ";
                                                command = command + nu_cMax.Value + ", ";
                                                command = command + idProv;

                                                BDConnection.execute(command);


                                                for (int i = 0; i < clb_zona.CheckedItems.Count; i++)
                                                {
                                                    string query = "EXEC CodingSpree.insertCiudXCup '";
                                                    query = query + clb_zona.CheckedItems[i] + "'";

                                                    BDConnection.execute(query);
                                                }

                                                MessageBox.Show("Cupon Armado");
                                                this.Hide();

                                            }
                                            else
                                            {
                                                MessageBox.Show("Fechas vigencia vencidas");
                                            }

                                        }
                                        else
                                        {
                                            MessageBox.Show("Seleccionar Zonas");
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show("Precio Real invalido");
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("Precio Ficticio Invalido");
                                }
                            }
                            else
                            {
                                MessageBox.Show("Cantidad Maxima Invalida");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Cantidad Disponible Invalida");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Fecha Vencimiento Consumo Invalida");
                    }
                }
                else
                {
                    MessageBox.Show("Fecha Vencimiento Oferta Invalida");
                }
            }
            else
            {
                MessageBox.Show("Descripcion Invalida");
            }


        }


    }


}
